package com.jzg.mvc.biz.cab;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.Cab;
import com.jzg.common.model.sys.SysOrg;
import com.jzg.common.render.RenderLayuiBean;

@Path(value = "/biz/cab", viewPath = "/views/biz/cab")
public class CabController extends BaseController{
	
	protected final Log LOG = Log.getLog(getClass());
	
	@Inject
	private CabService srv;
	
	public void index() {
		render("list.html");
	}
	
	public void listData() {
		RenderLayuiBean renderBean = srv.listData(this);
		this.renderJson(renderBean);
	}
	
	public void save () {
		Cab cab = getModel(Cab.class);
		
		if(cab==null) {
			renderError("参数不完整！");
			return;
		}
		
		SysOrg sysOrg = SysOrg.dao.findById(cab.getOrgId());
		if(sysOrg==null) {
			renderError("部门不存在，请刷新重试！");
			return;
		}
		
		String license;
		if(!StrKit.isBlank(sysOrg.getCODE())) {
			license = "PL-JZG-"+sysOrg.getCODE()+"-"+(int)((Math.random()*9+1)*1000);
		}else {
			license = "PL-JZG-"+"-"+(int)((Math.random()*9+1)*1000);
		}
		
		if (StrKit.notBlank(cab.getId())) {
			if (cab.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			cab.setId(license);
			cab.setLicense(license);
			if (cab.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}
		
	}
	
	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() throws Exception {
		String id = getPara("id");
		Cab cab = Cab.dao.findById(id);
		if (cab != null) {
			if (cab.delete()) {
				renderSuccess("删除成功！");
				srv.delete(cab);
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
}
